package com.xbai.content.controller;

import com.github.pagehelper.PageInfo;
import com.xbai.content.pojo.Content;
import com.xbai.content.service.ContentService;
import com.xbai.entity.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @desc ContentController
 * @author jxbai
 * @date 2021/1/28
 */

@RestController
@RequestMapping("/content")
@CrossOrigin
@RequiredArgsConstructor
public class ContentController {

    private final ContentService contentService;

    @GetMapping("/category/{id}")
    public Result<List<Content>> findByCategory(@PathVariable("id") Long categoryId) {
        List<Content> contentList = contentService.findByCategory(categoryId);
        return new Result<>(contentList);
    }

    /***
     * Content分页条件搜索实现
     * @param content {@link Content}
     * @param page 页码
     * @param size 页大小
     * @return {@link Result<PageInfo>}
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false) Content content, @PathVariable  int page, @PathVariable  int size){
        //调用ContentService实现分页条件查询Content
        PageInfo<Content> pageInfo = contentService.findPage(content, page, size);
        return new Result<>(pageInfo);
    }

    /***
     * Content分页搜索实现
     * @param page 页码
     * @param size 页大小
     * @return {@link Result<PageInfo>}
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用ContentService实现分页查询Content
        PageInfo<Content> pageInfo = contentService.findPage(page, size);
        return new Result<>(pageInfo);
    }

    /***
     * 多条件搜索Content数据
     * @param content {@link Content}
     * @return {@link Result}
     */
    @PostMapping(value = "/search" )
    public Result<List<Content>> findList(@RequestBody(required = false) Content content){
        //调用ContentService实现条件查询Content
        List<Content> list = contentService.findList(content);
        return new Result<>(list);
    }

    /***
     * 根据ID删除Content数据
     * @param id 主键id
     * @return {@link Result}
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Long id){
        //调用ContentService实现根据主键删除
        contentService.delete(id);
        return new Result();
    }

    /***
     * 修改Content数据
     * @param content {@link Content}
     * @param id 主键id
     * @return {@link Result}
     */
    @PutMapping(value="/{id}")
    public Result update(@RequestBody Content content, @PathVariable Long id){
        //设置主键值
        content.setId(id);
        //调用ContentService实现修改Content
        contentService.update(content);
        return new Result();
    }

    /***
     * 新增Content数据
     * @param content {@link Content}
     * @return {@link Result}
     */
    @PostMapping
    public Result add(@RequestBody Content content){
        //调用ContentService实现添加Content
        contentService.add(content);
        return new Result();
    }

    /***
     * 根据ID查询Content数据
     * @param id 主键id
     * @return {@link Result}
     */
    @GetMapping("/{id}")
    public Result<Content> findById(@PathVariable Long id){
        //调用ContentService实现根据主键查询Content
        Content content = contentService.findById(id);
        return new Result<>(content);
    }

    /***
     * 查询Content全部数据
     * @return {@link Result}
     */
    @GetMapping
    public Result<List<Content>> findAll(){
        //调用ContentService实现查询所有Content
        List<Content> list = contentService.findAll();
        return new Result<>(list) ;
    }
}
